
/******************************************************************************
 *
 *	PROJECT: Flynax Classifieds Software
 *	VERSION: 4.0
 *	LISENSE: FL87MPRYOA0R - http://www.flynax.com/license-agreement.html
 *	PRODUCT: Auto Classifieds
 *	DOMAIN: www.siyaradiali.com
 *	FILE: LIB.JS
 *
 *	This script is a commercial software and any kind of using it must be 
 *	coordinate with Flynax Owners Team and be agree to Flynax License Agreement
 *
 *	This block may not be removed from this file or any other files with out 
 *	permission of Flynax respective owners.
 *
 *	Copyrights Flynax Classifieds Software | 2012
 *	http://www.flynax.com/
 *
 ******************************************************************************/

var lfHandler = function(base){
	if ( base.markers[0] )
	{
		/* set marker draggable */
		base.markers[0].setDraggable(true);
		
		/* attache hint */
		var infowindow = new google.maps.InfoWindow({
			content: lfConfig['phrase_drag_hint'],
			maxWidth: 120
		});
		
		if ( !lfConfig['postZoom'] )
		{	
			infowindow.open(base.map, base.markers[0]);
		}
		else
		{
			lfUse(base);
		}
		
		/* set listeners */
		google.maps.event.addListener(base.markers[0], 'dragstart', function(){
			infowindow.close();
		});
		
		google.maps.event.addListener(base.markers[0], 'dragend', function(){
			lfUpdate(base);
			$('#lf_use').attr('checked', true);
		});
		
		google.maps.event.addListener(base.map, 'zoom_changed', function(){
			lfUpdate(base);
		});
		
		lfUpdate(base);
		
		$('#lf_use').change(function(){
			infowindow.close();
			lfUse(base);
		});
		
		/* search */
		$('#lf_search').click(function(){
			lfSearch(base);
		});
		$('#lf_query').keydown(function(event){
			if ( event.keyCode == 13 )
			{
				lfSearch(base);
				
				$(this).closest('form').attr('onsubmit', 'return false;');
			}
		});
		$('#lf_query').blur(function(){
			$(this).closest('form').attr('onsubmit', 'return true;');
		});
	}
};

var lfSearch = function(base)
{
	var request = $('#lf_query').val();
			
	if ( request )
	{
		lfConfig['geoCoder'].geocode({'address': request}, function(results, status) {
			if ( status == 'OK' )
			{
				var lat = results[0].geometry.location.lat();
				var lng = results[0].geometry.location.lng();
				var latLng = new google.maps.LatLng(lat, lng)
				
				base.markers[0].setPosition(latLng);
				base.map.setCenter(latLng);
				
				lfUpdate(base);
				//results[0].formatted_address
			}
			else
			{
				printMessage('warning', lfConfig['phrase_not_found'].replace('{location}', request));
			}
		});
	}
};

var lfUpdate = function(base){
	var zoom = base.map.getZoom();
	var lat = base.markers[0].getPosition().lat();
	var lng = base.markers[0].getPosition().lng();
	
	$('#lf_zoom').val(zoom);
	$('#lf_lat').val(lat);
	$('#lf_lng').val(lng);
};

var lfUse = function(base){
	base.markers[0].setVisible($('#lf_use').is(':checked'))
};